package com.zq.zhufengfm.fragments;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.zq.zhufengfm.adapter.BaseFragmentPagerAdapter;
import com.zq.zhufengfm.fragments.discover.DisCoverCategoryFragment;
import com.zq.zhufengfm.fragments.discover.DisCoverLiveFragment;
import com.zq.zhufengfm.fragments.discover.DisCoverRecommendFragment;
import com.zq.zhufengfm.fragments.discover.DiscoverAnchorFragment;
import com.zq.zhufengfm.fragments.discover.DiscoverRatingFragment;

import java.util.ArrayList;
import java.util.List;


public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener{

    private ViewPager viewPager ;

    public DiscoverFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);

//        //TabLayout的加载
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.discover_top_tab_bar);

        //2.ViewPager的加载
        viewPager = (ViewPager) view.findViewById(R.id.discover_view_pager);

        List<BaseFragment> fragments = new ArrayList<BaseFragment>();
        fragments.add(new DisCoverRecommendFragment());
        fragments.add(new DisCoverCategoryFragment());
        fragments.add(new DisCoverLiveFragment());
        fragments.add(new DiscoverRatingFragment());
        fragments.add(new DiscoverAnchorFragment());

        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(
                getChildFragmentManager(), fragments);

        viewPager.setAdapter(pagerAdapter);

        //代码添加： 封装了TabLayout 与ViewPager的联动。
        //需要  ViewPager内部指定的Adapter   必须要重写getPageTitle();
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);

        return view;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

}
